<template>
	<view class="forget">

		<view class="content">
			<!-- 主体 -->
			<view class="main">
				<view class="tips">若你忘记了密码，可在此重置新密码。</view>
				<wInput v-model="form['forgotPwd'].data.mobile" type="text" maxlength="11" placeholder="请输入手机号码"></wInput>
				<wInput v-model="form['forgotPwd'].data.password" type="password" maxlength="11" placeholder="请输入新密码"
					isShowPass></wInput>

				<wInput v-model="form['forgotPwd'].data.code" type="number" maxlength="4" placeholder="验证码" isShowCode
					codeText="获取重置码" setTime="30" ref="runCode" @setCode="getVerCode()"></wInput>
			</view>

			<wButton class="wbutton" text="重置密码" :rotate="isRotate" @click.native="startRePass()"></wButton>

		</view>
	</view>
</template>

<script>
	let _this;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button
	import FormValidate from "@/shopro/validate/form";
	import schema from "@/uview-ui/libs/util/async-validator";
	import wechat from "@/shopro/wechat/wechat";
	import {
		mapMutations,
		mapActions,
		mapState,
		mapGetters
	} from "vuex";
	export default {
		data() {
			return {
				form: {
					forgotPwd: {
						data: {
							mobile: "", //手機號
							code: "", //驗證碼
							password: "", //密碼
							isMobileEnd: false // 手機號輸入完畢
						},
						rules: {
							mobile: FormValidate.mobile,
							code: FormValidate.code,
							password: FormValidate.password
						},
						error: {
							mobile: "", //手機號
							code: "", //驗證碼
							password: "" //密碼
						}
					},
				},
				phoneData: "", //电话
				passData: "", //密码
				verCode: "", //验证码
				isRotate: false, //是否加载旋转
			}
		},
		components: {
			wInput,
			wButton
		},
		mounted() {
			_this = this;
		},
		methods: {
			// 獲取短信驗證碼
			getSmsCode(type) {
				const that = this;
				if (!that.disabledCode) {
					that.$http(
						"common.smsSend",
						{
							mobile: that.form['forgotPwd'].data.mobile,
							event: type
						},
						"獲取驗證碼中..."
					).then(res => {
						if (res.code === 1) {
							that.codeChange();
							that.$u.toast("驗證碼已發送，請註意查收短信");
						} else {
							that.$u.toast(res.msg);
						}
					});
				} else {
					that.$u.toast("請稍後再試");
				}
			},
			
			// 倒計時
			codeChange() {
				if (this.disabledCode) return;
				this.disabledCode = true;
				let n = 60;
				this.codeText = n + "s";
				const run = setInterval(() => {
					n -= 1;
					if (n < 0) {
						clearInterval(run);
					}
					this.codeText = n + "s";
					if (this.codeText < 0 + "s") {
						this.disabledCode = false;
						this.codeText = "重新獲取";
					}
				}, 1000);
			},
			getVerCode() {
				//获取验证码
				if (_this.form['forgotPwd'].data.mobile.length != 11) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '手机号不正确'
					});
					return false;
				}
				
				this.getSmsCode('resetpwd')
				console.log("获取验证码")
				this.$refs.runCode.$emit('runCode'); //触发倒计时（一般用于请求成功验证码后调用）
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '模拟倒计时触发'
				});

				setTimeout(function() {
					_this.$refs.runCode.$emit('runCode', 0); //假装模拟下需要 终止倒计时
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '模拟倒计时终止'
					});
				}, 3000)
			},
			startRePass() {
				//重置密码
				if (this.isRotate) {
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.form['forgotPwd'].data.mobile.length != 11) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '手机号不正确'
					});
					return false;
				}
				if (this.form['forgotPwd'].data.password.length < 6) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '密码不正确'
					});
					return false;
				}
				if (this.form['forgotPwd'].data.code.length != 4) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '验证码不正确'
					});
					return false;
				}
				console.log("重置密码成功")
				this
					.$http(
						"user.forgotPwd",
						{
							mobile: this.form["forgotPwd"].data.mobile,
							code: this.form["forgotPwd"].data.code,
							password: this.form["forgotPwd"].data.password
						},
						"修改中..."
					)
					.then(res => {
						if (res.code === 1) {
							this.$u.toast(res.msg);
							if (this.isLogin) {
								uni.switchTab({
									url:"/pages/index/user"
								})
								// this.closeAuthModal();
							} else {
								uni.redirectTo({
									url:"/pages/login/login"
								})
								// this.showAuthModal("accountLogin");
							}
						}
					});
				_this.isRotate = true
				setTimeout(function() {
					_this.isRotate = false
				}, 3000)


			}
		}
	}
</script>

<style>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");
</style>